package hotel.controller.employee;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import hotel.modal.Employee;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ChoiceBox;
public class EmployeeChangeController {
	@FXML private TextField phoneField;
	@FXML private TextField nameField;
	@FXML private TextField employeeIdField;
	@FXML private TextField certIdField;
	@FXML private TextField addressField;
	@FXML private DatePicker entryTimeDp;
	@FXML private DatePicker birthdayDp;
	@FXML private ChoiceBox<String> genderCh;
	@FXML private ChoiceBox<String> statusCh;
	@FXML private Button btnOk;
	@FXML private Button btnCancel;
	Employee employee = null;
	
	@FXML
    public void initialize() {
		/*entryTimeDp.setConverter(new StringConverter<LocalDate>() {
		     String pattern = "yyyy-MM-dd";
		     DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(pattern);
		     { entryTimeDp.setPromptText(pattern.toLowerCase()); }

		     @Override public String toString(LocalDate date) {
		         if (date != null) {
		             return dateFormatter.format(date);
		         } else return "";
		     }

		     @Override public LocalDate fromString(String string) {
		         if (string != null && !string.isEmpty()) {
		             return LocalDate.parse(string, dateFormatter);
		         } else return null;
		     }
		 });*/
		
	}
	
	public void initData(Employee employee) {
		this.employee = employee;
		if(employee != null && employee.getEmployeeId() != -1) {
			
			phoneField.setText(employee.getPhone());
			nameField.setText(employee.getName());
			employeeIdField.setText(String.valueOf(employee.getEmployeeId()));
			certIdField.setText(employee.getCertId());
			addressField.setText(employee.getAddress());
			if(employee.getGender().equals("F")) genderCh.getSelectionModel().select("女");
			else genderCh.getSelectionModel().select("男");
			if(employee.isStatus()) statusCh.getSelectionModel().select("在职");
			else statusCh.getSelectionModel().select("离职");
		    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		    birthdayDp.setValue(LocalDate.parse(employee.getBirthday(), dateFormatter));
		    entryTimeDp.setValue(LocalDate.parse(employee.getEntryTime(), dateFormatter));
		}
	}
	
	// Event Listener on Button[#btnOk].onAction
	@FXML
	public void onBtnOkAction(ActionEvent event) {
		employee.setPhone(phoneField.getText().trim());
		employee.setName(nameField.getText());
		employee.setAddress(addressField.getText().trim());
		//employee.setCertId(certIdField.getText());
		//employee.setEmployeeId(Integer.parseInt(employeeIdField.getText()));
		employee.setGender(genderCh.getSelectionModel().getSelectedItem().equals("男")?"M":"F");
		employee.setStatus(statusCh.getSelectionModel().getSelectedItem().equals("在职")?true:false);
		DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		employee.setBirthday(dateFormatter.format(birthdayDp.getValue()));
		employee.setEntryTime(dateFormatter.format(entryTimeDp.getValue()));
		employee.setModified(true);
		closeWindow();
	}
	
	// Event Listener on Button[#btnCancel].onAction
	@FXML
	public void onBtnCancelAction(ActionEvent event) {
		employee.setModified(false);
		closeWindow();
	}
	
	private void closeWindow() {
		Stage stage = (Stage) btnCancel.getScene().getWindow();
		stage.close();
	}
	
}
